package com.xyd.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xyd.springcloud.model.User;
import com.xyd.springcloud.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * @description
     *      *      consumer的controller调用service接口
     *      *      --->service接口映射provider中controller
     *      *      --->但是provider中的controller抛出异常
     *      *      --->调用selectAllBooks()备用方法
     *      *      @HystrixCommand指向后备的方法
     * @return
     * @throws Exception
     */
    @RequestMapping("/all")
    @HystrixCommand(fallbackMethod = "selectAllUsersFallBack")
    public List<User> selectAllUsers() throws Exception {
        List<User> list = userService.getAllUser();
        if (list.size() > 0) {
            throw new Exception("模拟异常，测试熔断处理！");
        }

        return list;
    }


    public List<User> selectAllUsersFallBack() {
        List<User> userList = new ArrayList<User>();
        User user = new User("张三三", "333", 33);
        userList.add(user);
        return userList;
    }
}
